﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class ucThietBiDienTu : UserControl
    {
        ThietBiDienBUS _ThietBiBUS = new ThietBiDienBUS();
        ThietBiDienDTO _ThietBiDTO = new ThietBiDienDTO();
        List<int> rPIN = new List<int>();
        List<int> rACQUY = new List<int>();
        List<int> rBOKICHDIEN = new List<int>();
        List<int> rBOSAC = new List<int>();
        int _IsSelected = 0;
        int i = 0;
        public ucThietBiDienTu()
        {
            InitializeComponent();
        }

        private void tsDanhSachThietBi_Click(object sender, EventArgs e)
        {
            pnDanhSach.Visible = true;
            pnDanhSach.Dock = DockStyle.Fill;
            pnThem.Visible = false;
        }

        private void tsThemThietBi_Click(object sender, EventArgs e)
        {
            pnThem.Dock = DockStyle.Fill;
            pnThem.Visible = true;
            pnDanhSach.Visible = false;
            string _cbbPIN = "select  b.MaLoaiThietBi,b.TenLoaiThietBi from THIETBIDIEN a,LOAITHIETBIDIEN b where a.MaLoaiThietBi = b.MaLoaiThietBi and a.TenThietBi = 'Pin' group by b.TenLoaiThietBi,b.MaLoaiThietBi";            
            DataTable dt = ThietBiDienBUS.DanhSach(_cbbPIN);
            cbbLoaiThietBi.DataSource = dt;            
            cbbLoaiThietBi.DisplayMember = "TenLoaiThietBi";
            cbbLoaiThietBi.ValueMember = "MaLoaiThietBi";
            string _cbbACQUY = "select  b.MaLoaiThietBi,b.TenLoaiThietBi from THIETBIDIEN a,LOAITHIETBIDIEN b where a.MaLoaiThietBi = b.MaLoaiThietBi and a.TenThietBi like 'Acquy%' group by b.TenLoaiThietBi,b.MaLoaiThietBi ";
            dt = ThietBiDienBUS.DanhSach(_cbbACQUY);
            cbbLoaiThietBiAcquy.DataSource = dt;
            cbbLoaiThietBiAcquy.DisplayMember = "TenLoaiThietBi";
            cbbLoaiThietBiAcquy.ValueMember = "MaLoaiThietBi";
            string _cbbSAC = "select  b.MaLoaiThietBi,b.TenLoaiThietBi from THIETBIDIEN a,LOAITHIETBIDIEN b where a.MaLoaiThietBi = b.MaLoaiThietBi and a.TenThietBi like N'%sạc%' group by b.TenLoaiThietBi,b.MaLoaiThietBi ";
            dt = ThietBiDienBUS.DanhSach(_cbbSAC);
            cbbLoaiThietBiSac.DataSource = dt;
            cbbLoaiThietBiSac.DisplayMember = "TenLoaiThietBi";
            cbbLoaiThietBiSac.ValueMember = "MaLoaiThietBi";
            string _cbbKICKDIEN = "select  b.MaLoaiThietBi,b.TenLoaiThietBi from THIETBIDIEN a,LOAITHIETBIDIEN b where a.MaLoaiThietBi = b.MaLoaiThietBi and a.TenThietBi like N'%kích điện%' group by b.TenLoaiThietBi,b.MaLoaiThietBi ";
            dt = ThietBiDienBUS.DanhSach(_cbbKICKDIEN);
            cbbLoaiThietBiKickDien.DataSource = dt;
            cbbLoaiThietBiKickDien.DisplayMember = "TenLoaiThietBi";
            cbbLoaiThietBiKickDien.ValueMember = "MaLoaiThietBi";
        }

        private void ucThietBiDienTu_Load(object sender, EventArgs e)
        {
            pnThem.Visible = false;
            pnDanhSach.Dock = DockStyle.Fill;
            DataTable dt = ThietBiDienBUS.LayDanhSachPIN();
            dgvPin.DataSource = dt;
            dt = ThietBiDienBUS.LayDanhSachACQUY();
            dgvAcquy.DataSource = dt;
            dt = ThietBiDienBUS.LayDanhSachBOKICHDIEN();
            dgvBKDien.DataSource = dt;
            dt = ThietBiDienBUS.LayDanhSachBOSAC();
            dgvDKSac.DataSource = dt;
        }

        public void XuLy()
        {
            tsThemThietBi.Enabled = true;
            dgvPin.ReadOnly = dgvDKSac.ReadOnly = dgvAcquy.ReadOnly = dgvBKDien.ReadOnly = false;
            btnCapNhat_Acquy.Enabled = btnCapNhat_BKDien.Enabled = btnCapNhat_DKSac.Enabled = btnCapNhat_Pin.Enabled = true;
        }

        private void btnThoat2_Pin_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            txtChieuDai.Text = "";
            txtChieuDay.Text = "";
            txtChieuRong.Text = "";
            txtCongSuat.Text = "";
            txtDonGia.Text = "";
            txtImpA.Text = "";
            txtIscA.Text = "";
            txtTenThietBi.Text = "";
            txtVmpV.Text = "";
            txtVocV.Text = "";
            textBox1.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox14.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";            
            //txtTenThietBi.Focus();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            switch (_IsSelected)
            {
                case 0:
                    if (dgvChiTietThietBiPIN.RowCount > 1)
                    {
                        ThietBiDienDTO _ThietBiDien = new ThietBiDienDTO();

                        int i = 0;
                        while (dgvChiTietThietBiPIN.RowCount > 1)
                        {
                            if (dgvChiTietThietBiPIN.Rows[i].Cells[0].Value == null)
                            {
                                dgvChiTietThietBiPIN.Rows.RemoveAt(i);
                            }
                            else
                            {
                                _ThietBiDien.TenThietBi = dgvChiTietThietBiPIN.Rows[i].Cells[0].Value.ToString();
                                _ThietBiDien.LoaiThietBi = dgvChiTietThietBiPIN.Rows[i].Cells[1].Value.ToString();
                                _ThietBiDien.DonGia = double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[2].Value.ToString());
                                _ThietBiDien.CongSuat = double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[3].Value.ToString());
                                string _kichthuoc = dgvChiTietThietBiPIN.Rows[i].Cells[4].Value.ToString();
                                _kichthuoc += "x";
                                _kichthuoc += dgvChiTietThietBiPIN.Rows[i].Cells[5].Value.ToString();
                                _kichthuoc += "x";
                                _kichthuoc += dgvChiTietThietBiPIN.Rows[i].Cells[6].Value.ToString();
                                _ThietBiDien.KickThuoc = _kichthuoc;
                                _ThietBiDien.Vmp= double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[7].Value.ToString());
                                _ThietBiDien.Imp= double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[8].Value.ToString());
                                _ThietBiDien.Isc= double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[9].Value.ToString());
                                _ThietBiDien.Voc= double.Parse(dgvChiTietThietBiPIN.Rows[i].Cells[10].Value.ToString());
                                ThietBiDienBUS.ThemThietBiPIN(_ThietBiDien);
                                dgvChiTietThietBiPIN.Rows.RemoveAt(i);
                            }
                        }
                        if (dgvChiTietThietBiPIN.RowCount == 1)
                        {
                            MessageBox.Show("Thêm thành công");
                            DataTable dt = ThietBiDienBUS.LayDanhSachPIN();
                            dgvPin.DataSource = dt;
                        }
                    }
                    break;
                case 1:
                    if (dataGridView1.RowCount > 1)
                    {
                        ThietBiDienDTO _ThietBiDien = new ThietBiDienDTO();

                        int i = 0;
                        while (dataGridView1.RowCount > 1)
                        {
                            if (dataGridView1.Rows[i].Cells[0].Value == null)
                            {
                                dataGridView1.Rows.RemoveAt(i);
                            }
                            else
                            {
                                _ThietBiDien.TenThietBi = dataGridView1.Rows[i].Cells[0].Value.ToString();
                                _ThietBiDien.LoaiThietBi = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                _ThietBiDien.DonGia = double.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                                _ThietBiDien.CongSuat = double.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                                string _kichthuoc = dataGridView1.Rows[i].Cells[4].Value.ToString();
                                _kichthuoc += "x";
                                _kichthuoc += dataGridView1.Rows[i].Cells[5].Value.ToString();
                                _kichthuoc += "x";
                                _kichthuoc += dataGridView1.Rows[i].Cells[6].Value.ToString();
                                _ThietBiDien.KickThuoc = _kichthuoc;
                                _ThietBiDien.Vmp = double.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString());
                                _ThietBiDien.Imp = double.Parse(dataGridView1.Rows[i].Cells[7].Value.ToString());                                
                                ThietBiDienBUS.ThemThietBiACQUY(_ThietBiDien);
                                dataGridView1.Rows.RemoveAt(i);
                            }
                        }
                        if (dataGridView1.RowCount == 1)
                        {
                            MessageBox.Show("Thêm thành công");
                            DataTable dt = ThietBiDienBUS.LayDanhSachACQUY();
                            dgvAcquy.DataSource = dt;
                        }
                    }
                    break;
                case 2:
                    if (dataGridView2.RowCount > 1)
                    {
                        ThietBiDienDTO _ThietBiDien = new ThietBiDienDTO();

                        int i = 0;
                        while (dataGridView2.RowCount > 1)
                        {
                            if (dataGridView2.Rows[i].Cells[0].Value == null)
                            {
                                dataGridView2.Rows.RemoveAt(i);
                            }
                            else
                            {
                                _ThietBiDien.TenThietBi = dataGridView2.Rows[i].Cells[0].Value.ToString();
                                _ThietBiDien.LoaiThietBi = dataGridView2.Rows[i].Cells[1].Value.ToString();
                                _ThietBiDien.DonGia = double.Parse(dataGridView2.Rows[i].Cells[2].Value.ToString());
                                _ThietBiDien.CongSuat = double.Parse(dataGridView2.Rows[i].Cells[3].Value.ToString());
                                ThietBiDienBUS.ThemThietBiBOKICHDIEN(_ThietBiDien);
                                dataGridView2.Rows.RemoveAt(i);
                            }
                        }
                        if (dataGridView2.RowCount == 1)
                        {
                            MessageBox.Show("Thêm thành công");
                            DataTable dt = ThietBiDienBUS.LayDanhSachBOKICHDIEN();
                            dgvBKDien.DataSource = dt;
                        }
                    }
                    break;
                case 3:
                    if (dataGridView3.RowCount > 1)
                    {
                        ThietBiDienDTO _ThietBiDien = new ThietBiDienDTO();

                        int i = 0;
                        while (dataGridView3.RowCount > 1)
                        {
                            if (dataGridView3.Rows[i].Cells[0].Value == null)
                            {
                                dataGridView3.Rows.RemoveAt(i);
                            }
                            else
                            {
                                _ThietBiDien.TenThietBi = dataGridView3.Rows[i].Cells[0].Value.ToString();
                                _ThietBiDien.LoaiThietBi = dataGridView3.Rows[i].Cells[1].Value.ToString();
                                _ThietBiDien.DonGia = double.Parse(dataGridView3.Rows[i].Cells[2].Value.ToString());
                                _ThietBiDien.CongSuat = double.Parse(dataGridView3.Rows[i].Cells[3].Value.ToString());
                                ThietBiDienBUS.ThemThietBiBOSAC(_ThietBiDien);
                                dataGridView3.Rows.RemoveAt(i);
                            }
                        }
                        if (dataGridView2.RowCount == 1)
                        {
                            MessageBox.Show("Thêm thành công");
                            DataTable dt = ThietBiDienBUS.LayDanhSachBOSAC();
                            dgvDKSac.DataSource = dt;
                        }
                    }
                    break;
            }
            
        }

        private int HopLe(DataGridView data, string str, int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void btnNhap_Click(object sender, EventArgs e)
        {
            i = dgvChiTietThietBiPIN.RowCount;
            i--;
            if (txtChieuDai.Text.ToString() != "" && txtChieuDay.Text.ToString() != "" && txtChieuRong.Text.ToString() != "" && txtCongSuat.Text.ToString() != ""
             && txtDonGia.Text.ToString() != "" && txtImpA.Text.ToString() != "")
            {
                if (HopLe(dgvChiTietThietBiPIN, txtTenThietBi.Text.ToString(), i) == 1)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    dgvChiTietThietBiPIN.Rows.Add(row);
                    int count = dgvChiTietThietBiPIN.RowCount;
                    //MessageBox.Show(count.ToString());
                    dgvChiTietThietBiPIN.Rows[i].Cells[0].Value = txtTenThietBi.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[1].Value = cbbLoaiThietBi.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[2].Value = txtDonGia.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[3].Value = txtCongSuat.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[4].Value = txtChieuDai.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[5].Value = txtChieuRong.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[6].Value = txtChieuDay.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[7].Value = txtVmpV.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[8].Value = txtImpA.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[9].Value = txtIscA.Text.ToString();
                    dgvChiTietThietBiPIN.Rows[i].Cells[10].Value = txtVocV.Text.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Thiết bị đã thêm ở bên dưới");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của thiết bị");
                txtTenThietBi.Focus();
            }
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                    e.Handled = true;
            }
            catch { };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = dataGridView1.RowCount;
            i--;
            if (textBox6.Text.ToString() != "" && textBox2.Text.ToString() != "" && textBox1.Text.ToString() != "" && textBox9.Text.ToString() != ""
             && textBox8.Text.ToString() != "" && textBox4.Text.ToString() != "" && textBox5.Text.ToString() != "" && textBox10.Text.ToString() != "")
            {
                if (HopLe(dataGridView1, textBox6.Text.ToString(), i) == 1)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    dataGridView1.Rows.Add(row);
                    int count = dataGridView1.RowCount;
                    //MessageBox.Show(count.ToString());
                    dataGridView1.Rows[i].Cells[0].Value = textBox6.Text.ToString();
                    dataGridView1.Rows[i].Cells[1].Value = cbbLoaiThietBiAcquy.Text.ToString();
                    dataGridView1.Rows[i].Cells[2].Value = textBox2.Text.ToString();
                    dataGridView1.Rows[i].Cells[3].Value = textBox1.Text.ToString();
                    dataGridView1.Rows[i].Cells[4].Value = textBox9.Text.ToString();
                    dataGridView1.Rows[i].Cells[5].Value = textBox8.Text.ToString();
                    dataGridView1.Rows[i].Cells[6].Value = textBox4.Text.ToString();
                    dataGridView1.Rows[i].Cells[7].Value = textBox5.Text.ToString();
                    dataGridView1.Rows[i].Cells[8].Value = textBox10.Text.ToString();                    
                    i++;
                }
                else
                {
                    MessageBox.Show("Thiết bị đã thêm ở bên dưới");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của thiết bị");
                textBox6.Focus();
            }            
        }

        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
        {
            _IsSelected = tabControl2.SelectedIndex;
            //MessageBox.Show(_IsSelected.ToString());            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            i = dataGridView2.RowCount;
            i--;
            if (textBox11.Text.ToString() != "" && textBox7.Text.ToString() != "" && textBox3.Text.ToString() != "" )
            {
                if (HopLe(dataGridView2, textBox11.Text.ToString(), i) == 1)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    dataGridView2.Rows.Add(row);
                    int count = dataGridView2.RowCount;
                    //MessageBox.Show(count.ToString());
                    dataGridView2.Rows[i].Cells[0].Value = textBox11.Text.ToString();
                    dataGridView2.Rows[i].Cells[1].Value = cbbLoaiThietBiKickDien.Text.ToString();
                    dataGridView2.Rows[i].Cells[2].Value = textBox7.Text.ToString();
                    dataGridView2.Rows[i].Cells[3].Value = textBox3.Text.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Thiết bị đã thêm ở bên dưới");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của thiết bị");
                textBox6.Focus();
            }            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            i = dataGridView3.RowCount;
            i--;
            if (textBox14.Text.ToString() != "" && textBox13.Text.ToString() != "" && textBox12.Text.ToString() != "")
            {
                if (HopLe(dataGridView3, textBox14.Text.ToString(), i) == 1)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    dataGridView3.Rows.Add(row);
                    int count = dataGridView3.RowCount;
                    //MessageBox.Show(count.ToString());
                    dataGridView3.Rows[i].Cells[0].Value = textBox14.Text.ToString();
                    dataGridView3.Rows[i].Cells[1].Value = cbbLoaiThietBiSac.Text.ToString();
                    dataGridView3.Rows[i].Cells[2].Value = textBox13.Text.ToString();
                    dataGridView3.Rows[i].Cells[3].Value = textBox12.Text.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Thiết bị đã thêm ở bên dưới");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin của thiết bị");
                textBox6.Focus();
            }            
        }

        private void dgvPin_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        { 
            int r = dgvPin.CurrentCell.RowIndex;
            _ThietBiDTO.TenThietBi= dgvPin.Rows[r].Cells[0].Value.ToString();            
            _ThietBiDTO.LoaiThietBi = dgvPin.Rows[r].Cells[1].Value.ToString();
            _ThietBiDTO.DonGia = double.Parse(dgvPin.Rows[r].Cells[2].Value.ToString());
            _ThietBiDTO.CongSuat = double.Parse(dgvPin.Rows[r].Cells[3].Value.ToString());
            _ThietBiDTO.KickThuoc = dgvPin.Rows[r].Cells[4].Value.ToString();
            _ThietBiDTO.Imp = double.Parse(dgvPin.Rows[r].Cells[5].Value.ToString());
            _ThietBiDTO.Vmp = double.Parse(dgvPin.Rows[r].Cells[6].Value.ToString());
            _ThietBiDTO.Isc = double.Parse(dgvPin.Rows[r].Cells[7].Value.ToString());
            _ThietBiDTO.Voc = double.Parse(dgvPin.Rows[r].Cells[8].Value.ToString());                
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _IsSelected = tabControl1.SelectedIndex;
        }

        private void dgvAcquy_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int i = dgvAcquy.CurrentCell.RowIndex;
            //MessageBox.Show(dgvAcquy.Rows[i].Cells[5].Value.ToString());
            _ThietBiDTO.TenThietBi = dgvAcquy.Rows[i].Cells[0].Value.ToString();
            _ThietBiDTO.LoaiThietBi = dgvAcquy.Rows[i].Cells[1].Value.ToString();
            _ThietBiDTO.DonGia = double.Parse(dgvAcquy.Rows[i].Cells[2].Value.ToString());
            if (dgvAcquy.Rows[i].Cells[3].Value.ToString() != "")
            {
                _ThietBiDTO.CongSuat = double.Parse(dgvAcquy.Rows[i].Cells[3].Value.ToString());
            }
            _ThietBiDTO.KickThuoc = dgvAcquy.Rows[i].Cells[4].Value.ToString();
            _ThietBiDTO.Imp = double.Parse(dgvAcquy.Rows[i].Cells[5].Value.ToString());
            _ThietBiDTO.Vmp = double.Parse(dgvAcquy.Rows[i].Cells[6].Value.ToString());
            _ThietBiDTO.MaLoaiThietBi = int.Parse(dgvAcquy.Rows[i].Cells[7].Value.ToString());
        }

        private void dgvBKDien_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int j = dgvBKDien.CurrentCell.RowIndex;
            //MessageBox.Show(dgvBKDien.Rows[j].Cells[0].Value.ToString());
            _ThietBiDTO.TenThietBi = dgvBKDien.Rows[j].Cells[0].Value.ToString();
            _ThietBiDTO.LoaiThietBi = dgvBKDien.Rows[j].Cells[1].Value.ToString();
            _ThietBiDTO.DonGia = double.Parse(dgvBKDien.Rows[j].Cells[2].Value.ToString());
            if (dgvBKDien.Rows[j].Cells[3].Value != null)
            {
                _ThietBiDTO.CongSuat = double.Parse(dgvBKDien.Rows[j].Cells[3].Value.ToString());
            }            
            _ThietBiDTO.MaLoaiThietBi = int.Parse(dgvBKDien.Rows[j].Cells[4].Value.ToString());
        }

        private void dgvDKSac_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int k = dgvDKSac.CurrentCell.RowIndex;
            //MessageBox.Show(dgvDKSac.Rows[k].Cells[0].Value.ToString());
            _ThietBiDTO.TenThietBi = dgvDKSac.Rows[k].Cells[0].Value.ToString();
            _ThietBiDTO.LoaiThietBi = dgvDKSac.Rows[k].Cells[1].Value.ToString();
            _ThietBiDTO.DonGia = double.Parse(dgvDKSac.Rows[k].Cells[2].Value.ToString());
            if (dgvDKSac.Rows[k].Cells[3].Value.ToString() != "")
            {
                _ThietBiDTO.CongSuat = double.Parse(dgvDKSac.Rows[k].Cells[3].Value.ToString());
            }
            _ThietBiDTO.MaLoaiThietBi = int.Parse(dgvDKSac.Rows[k].Cells[4].Value.ToString());
        }

        private void dgvPin_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvPin.Rows[e.RowIndex].Cells[0].Value.ToString() == _ThietBiDTO.TenThietBi)
                rPIN.Add(e.RowIndex);
            else if (ThietBiDienBUS.KiemTraTBDT(dgvPin.Rows[e.RowIndex].Cells[0].Value.ToString()) == 1)
                rPIN.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvPin.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                dgvPin.Rows[e.RowIndex].Cells[0].Value = _ThietBiDTO.TenThietBi;
            }
            switch (e.ColumnIndex)
            {
                case 0:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.TenThietBi;
                    break;
                case 1:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.LoaiThietBi;
                    break;
                case 2:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.DonGia;                        
                    break;
                case 3:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.CongSuat;
                    break;
                case 4:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.KickThuoc;
                    break;
                case 5:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.Imp;
                    break;
                case 6:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.Vmp;
                    break;
                case 7:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.Isc;
                    break;
                case 8:
                    if (dgvPin.CurrentCell.Value == DBNull.Value)
                        dgvPin.CurrentCell.Value = _ThietBiDTO.Voc;
                    break;
            }
        }

        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kiem tra
            TextBox txt = sender as TextBox;
            if (txt.Name == "Col")
            {
                try
                {
                    //Không đc nhập chữ và cho phép sử dụng dấu '.', backspace
                    if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)) && e.KeyChar != '.')
                        e.Handled = true;
                }
                catch { };
            }
        }

        private void dgvPin_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[2].DisplayIndex ||
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[3].DisplayIndex ||                
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[5].DisplayIndex ||
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[6].DisplayIndex ||
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[7].DisplayIndex ||
                this.dgvPin.CurrentCellAddress.X == dgvPin.Columns[8].DisplayIndex )
            {

                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void btnCapNhat_Pin_Click(object sender, EventArgs e)
        {
            ThietBiDienDTO tbtd = new ThietBiDienDTO();
            for (int i = 0; i < rPIN.Count; i++)
            {
                tbtd.TenThietBi = dgvPin.Rows[rPIN[i]].Cells[0].Value.ToString();
                tbtd.LoaiThietBi = dgvPin.Rows[rPIN[i]].Cells[1].Value.ToString();
                tbtd.DonGia = double.Parse(dgvPin.Rows[rPIN[i]].Cells[2].Value.ToString());
                tbtd.CongSuat = double.Parse(dgvPin.Rows[rPIN[i]].Cells[3].Value.ToString());
                tbtd.KickThuoc = dgvPin.Rows[rPIN[i]].Cells[4].Value.ToString();
                //MessageBox.Show(dgvPin.Rows[rPIN[i]].Cells[4].Value.ToString());
                tbtd.Imp = double.Parse(dgvPin.Rows[rPIN[i]].Cells[5].Value.ToString());
                tbtd.Vmp = double.Parse(dgvPin.Rows[rPIN[i]].Cells[6].Value.ToString());
                tbtd.Isc = double.Parse(dgvPin.Rows[rPIN[i]].Cells[7].Value.ToString());
                tbtd.Voc = double.Parse(dgvPin.Rows[rPIN[i]].Cells[8].Value.ToString());
                tbtd.MaLoaiThietBi = int.Parse(dgvPin.Rows[rPIN[i]].Cells[9].Value.ToString());
                //MessageBox.Show(dgvPin.Rows[rPIN[i]].Cells[9].Value.ToString());
                ThietBiDienBUS.CapNhatTBPIN(tbtd);
            }
            DataTable dt = ThietBiDienBUS.LayDanhSachPIN();
            dgvPin.DataSource = dt;
        }

        private void dgvAcquy_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvAcquy.Rows[e.RowIndex].Cells[0].Value.ToString() == _ThietBiDTO.TenThietBi)
                rACQUY.Add(e.RowIndex);
            else if (ThietBiDienBUS.KiemTraTBDT(dgvAcquy.Rows[e.RowIndex].Cells[0].Value.ToString()) == 1)
                rACQUY.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvAcquy.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                dgvAcquy.Rows[e.RowIndex].Cells[0].Value = _ThietBiDTO.TenThietBi;
            }
            switch (e.ColumnIndex)
            {
                case 0:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.TenThietBi;
                    break;
                case 1:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.LoaiThietBi;
                    break;
                case 2:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.DonGia;
                    break;
                case 3:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.CongSuat;
                    break;
                case 4:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.KickThuoc;
                    break;
                case 5:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.Imp;
                    break;
                case 6:
                    if (dgvAcquy.CurrentCell.Value == DBNull.Value)
                        dgvAcquy.CurrentCell.Value = _ThietBiDTO.Vmp;
                    break;                
            }
        }

        private void dgvBKDien_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvBKDien.Rows[e.RowIndex].Cells[0].Value.ToString() == _ThietBiDTO.TenThietBi)
                rBOKICHDIEN.Add(e.RowIndex);
            else if (ThietBiDienBUS.KiemTraTBDT(dgvBKDien.Rows[e.RowIndex].Cells[0].Value.ToString()) == 1)
                rBOKICHDIEN.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvBKDien.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                dgvBKDien.Rows[e.RowIndex].Cells[0].Value = _ThietBiDTO.TenThietBi;
            }
            switch (e.ColumnIndex)
            {
                case 0:
                    if (dgvBKDien.CurrentCell.Value == DBNull.Value)
                        dgvBKDien.CurrentCell.Value = _ThietBiDTO.TenThietBi;
                    break;
                case 1:
                    if (dgvBKDien.CurrentCell.Value == DBNull.Value)
                        dgvBKDien.CurrentCell.Value = _ThietBiDTO.LoaiThietBi;
                    break;
                case 2:
                    if (dgvBKDien.CurrentCell.Value == DBNull.Value)
                        dgvBKDien.CurrentCell.Value = _ThietBiDTO.DonGia;
                    break;
                case 3:
                    if (dgvBKDien.CurrentCell.Value == DBNull.Value)
                        dgvBKDien.CurrentCell.Value = _ThietBiDTO.CongSuat;
                    break;                
            }
        }

        private void dgvDKSac_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDKSac.Rows[e.RowIndex].Cells[0].Value.ToString() == _ThietBiDTO.TenThietBi)
                rBOSAC.Add(e.RowIndex);
            else if (ThietBiDienBUS.KiemTraTBDT(dgvDKSac.Rows[e.RowIndex].Cells[0].Value.ToString()) == 1)
                rBOSAC.Add(e.RowIndex);
            else
            {
                MessageBox.Show(dgvDKSac.Rows[e.RowIndex].Cells[0].Value.ToString() + " đã có");
                dgvDKSac.Rows[e.RowIndex].Cells[0].Value = _ThietBiDTO.TenThietBi;
            }
            switch (e.ColumnIndex)
            {
                case 0:
                    if (dgvDKSac.CurrentCell.Value == DBNull.Value)
                        dgvDKSac.CurrentCell.Value = _ThietBiDTO.TenThietBi;
                    break;
                case 1:
                    if (dgvDKSac.CurrentCell.Value == DBNull.Value)
                        dgvDKSac.CurrentCell.Value = _ThietBiDTO.LoaiThietBi;
                    break;
                case 2:
                    if (dgvDKSac.CurrentCell.Value == DBNull.Value)
                        dgvDKSac.CurrentCell.Value = _ThietBiDTO.DonGia;
                    break;
                case 3:
                    if (dgvDKSac.CurrentCell.Value == DBNull.Value)
                        dgvDKSac.CurrentCell.Value = _ThietBiDTO.CongSuat;
                    break;
            }
        }

        private void dgvAcquy_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (
                this.dgvAcquy.CurrentCellAddress.X == dgvAcquy.Columns[2].DisplayIndex ||
                this.dgvAcquy.CurrentCellAddress.X == dgvAcquy.Columns[3].DisplayIndex ||
                this.dgvAcquy.CurrentCellAddress.X == dgvAcquy.Columns[5].DisplayIndex ||
                this.dgvAcquy.CurrentCellAddress.X == dgvAcquy.Columns[6].DisplayIndex )                                
            {

                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void dgvBKDien_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (
                this.dgvBKDien.CurrentCellAddress.X == dgvBKDien.Columns[2].DisplayIndex ||
                this.dgvBKDien.CurrentCellAddress.X == dgvBKDien.Columns[3].DisplayIndex )
            {
                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void dgvDKSac_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = e.Control as TextBox;
            txt.Name = "";
            if (
                this.dgvDKSac.CurrentCellAddress.X == dgvDKSac.Columns[2].DisplayIndex ||
                this.dgvDKSac.CurrentCellAddress.X == dgvDKSac.Columns[3].DisplayIndex)
            {
                txt.Name = "Col";
                if (txt != null)
                {
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                }
            } 
        }

        private void btnCapNhat_DKSac_Click(object sender, EventArgs e)
        {
            ThietBiDienDTO tbtd = new ThietBiDienDTO();
            for (int i = 0; i < rBOSAC.Count; i++)
            {
                tbtd.TenThietBi = dgvDKSac.Rows[rBOSAC[i]].Cells[0].Value.ToString();
                tbtd.LoaiThietBi = dgvDKSac.Rows[rBOSAC[i]].Cells[1].Value.ToString();
                tbtd.DonGia = double.Parse(dgvDKSac.Rows[rBOSAC[i]].Cells[2].Value.ToString());
                if (dgvDKSac.Rows[rBOSAC[i]].Cells[3].Value.ToString() != "")
                {
                    tbtd.CongSuat = double.Parse(dgvDKSac.Rows[rBOSAC[i]].Cells[3].Value.ToString());                
                }
                tbtd.MaLoaiThietBi = int.Parse(dgvDKSac.Rows[rBOSAC[i]].Cells[4].Value.ToString());                
                ThietBiDienBUS.CapNhatTBBOSAC(tbtd);
            }
            DataTable dt = ThietBiDienBUS.LayDanhSachBOSAC();
            dgvDKSac.DataSource = dt;
        }

        private void btnCapNhat_Acquy_Click(object sender, EventArgs e)
        {
            ThietBiDienDTO tbtd = new ThietBiDienDTO();
            for (int i = 0; i < rACQUY.Count; i++)
            {
                tbtd.TenThietBi = dgvAcquy.Rows[rACQUY[i]].Cells[0].Value.ToString();
                tbtd.LoaiThietBi = dgvAcquy.Rows[rACQUY[i]].Cells[1].Value.ToString();
                tbtd.DonGia = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[2].Value.ToString());
                if (dgvAcquy.Rows[rACQUY[i]].Cells[3].Value.ToString() != "")
                {
                    tbtd.CongSuat = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[3].Value.ToString());
                }
                tbtd.KickThuoc = dgvAcquy.Rows[rACQUY[i]].Cells[4].Value.ToString();
                //MessageBox.Show(dgvAcquy.Rows[rACQUY[i]].Cells[4].Value.ToString());
                tbtd.Imp = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[5].Value.ToString());
                tbtd.Vmp = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[6].Value.ToString());
                //tbtd.Isc = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[7].Value.ToString());
                //tbtd.Voc = double.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[8].Value.ToString());
                tbtd.MaLoaiThietBi = int.Parse(dgvAcquy.Rows[rACQUY[i]].Cells[7].Value.ToString());
                //MessageBox.Show(dgvAcquy.Rows[rACQUY[i]].Cells[9].Value.ToString());
                ThietBiDienBUS.CapNhatTBACQUY(tbtd);
            }
            DataTable dt = ThietBiDienBUS.LayDanhSachACQUY();
            dgvAcquy.DataSource = dt;
        }

        private void btnCapNhat_BKDien_Click(object sender, EventArgs e)
        {
            ThietBiDienDTO tbtd = new ThietBiDienDTO();
            for (int i = 0; i < rBOKICHDIEN.Count; i++)
            {
                tbtd.TenThietBi = dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[0].Value.ToString();
                tbtd.LoaiThietBi = dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[1].Value.ToString();
                tbtd.DonGia = double.Parse(dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[2].Value.ToString());
                if (dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[3].Value != null)
                {
                    tbtd.CongSuat = double.Parse(dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[3].Value.ToString());
                }
                tbtd.MaLoaiThietBi = int.Parse(dgvBKDien.Rows[rBOKICHDIEN[i]].Cells[4].Value.ToString());
                ThietBiDienBUS.CapNhatTBBOKICHDIEN(tbtd);
            }
            DataTable dt = ThietBiDienBUS.LayDanhSachBOKICHDIEN();
            dgvBKDien.DataSource = dt;
        }


    }
}
